//
//  YZLCarRedioPlayerViewController.m
//  Car
//
//  Created by dllo on 15/11/4.
//  Copyright © 2015年 岳明. All rights reserved.
//

#import "YZLCarRedioPlayerViewController.h"
#import "HTTPTOOL.h"
#import <UIImageView+WebCache.h>
#import "YZLCarRedioListModel.h"
#import "YZLCarRedioListCell.h"
#import "YZLCarRedioPlayingViewController.h"

@interface YZLCarRedioPlayerViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UIActivityIndicatorView *activityView;//指示器
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, strong) NSMutableArray *ModelArr;
@property (nonatomic, strong) UIImageView *cycleImage;
@property (nonatomic, strong) NSIndexPath *selectedIndexPath;
@property (nonatomic, copy) NSString *result;
@property (nonatomic, copy) NSString *index;

@end

@implementation YZLCarRedioPlayerViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}

#pragma mark - data
- (void)dataHandle {
    
    NSString *str = [NSString stringWithFormat:@"http://app.api.autohome.com.cn/autov5.0.0/News/newsonewradiolist-pm1-aid%@-p1-s50-tp0.json", self.album_idStr];
    
    [HTTPTOOL GETWithURL:str body:nil httpHead:nil responseStyle:JSON success:^(id result) {

        NSArray *arr = [[result objectForKey:@"result"] objectForKey:@"list"];
        self.ModelArr = [NSMutableArray array];
        for (NSDictionary *dic in arr) {
            
            self.model = [YZLCarRedioListModel CarRedioListWithDic:dic];
            [self.ModelArr addObject:self.model];
            
        }

        [self.tableView reloadData];
        [self.activityView stopAnimating];
        
    } fail:^(NSError *error) {
        
    }];
}

#pragma mark - tableview
- (void)createTableView {
    
    self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.rowHeight = 55;
    
    //headerView
    self.headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 310)];
    self.tableView.tableHeaderView = self.headerView;
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 270)];
    [imageView sd_setImageWithURL:[NSURL URLWithString:self.album_bigImgurlStr]];
    
    // 磨砂(blur)效果
    UIVisualEffectView *visual = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
    visual.frame = imageView.frame;
    visual.alpha = 0.95f;
    [imageView addSubview:visual];
    [self.headerView addSubview:imageView];
    
    //列表
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 280, 200, 20)];
    label.text = @"全部列表";
    [self.headerView addSubview:label];
    
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(15, 310, self.view.frame.size.width - 15, 0.5)];
    view1.backgroundColor = [UIColor lightGrayColor];
    [self.headerView addSubview:view1];
    
    //headerView 电台图
    self.cycleImage = [[UIImageView alloc] initWithFrame:CGRectMake((self.view.frame.size.width - 120) / 2.f, 60, 120, 120)];
    [self.cycleImage sd_setImageWithURL:[NSURL URLWithString:self.album_bigImgurlStr]];
    self.cycleImage.layer.cornerRadius = 60;
    self.cycleImage.layer.masksToBounds = YES;
    [self.headerView addSubview:self.cycleImage];
    
    //电台描述
    UILabel *describeLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 185, self.view.frame.size.width - 140, 80)];
    describeLabel.text = self.album_descritionStr;
    describeLabel.textColor = [UIColor whiteColor];
//    describeLabel.backgroundColor = [UIColor redColor];
    describeLabel.font = [UIFont systemFontOfSize:15];
    describeLabel.numberOfLines = 0;
    [self.headerView addSubview:describeLabel];
}

#pragma mark -
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor whiteColor];
//    self.title = self.album_name;
    
    [self createTableView];
    [self dataHandle];
    
    //标题
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake((self.view.frame.size.width - 150) / 2.f, 30, 150, 20)];
    label.text = self.album_name;
    [self.headerView addSubview:label];
    label.textColor = [UIColor whiteColor];
    label.textAlignment = 1;
    
    //返回
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(20, 30, 30, 30);
    [button setImage:[UIImage imageNamed:@"button"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(returnButton) forControlEvents:UIControlEventTouchUpInside];
    [self.headerView addSubview:button];
    
    //bar颜色
    [self navigationbarColor];
    
    //指示器
    self.activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    self.activityView.color = [UIColor lightGrayColor];
    self.activityView.center = CGPointMake(self.view.frame.size.width / 2.f, (self.view.frame.size.height - 384) / 2.f + 320 - 64);
    [self.tableView addSubview:self.activityView];
    [self.activityView startAnimating];
    
    //通知中心
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(indexPath:) name:@"indexPath" object:nil];
    //将存入的本地的字符串读取
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *filePath = [documentPath stringByAppendingPathComponent:@"redio.txt"];
    self.result = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
    NSLog(@"🌹 %@", self.result);

    
}

- (void)returnButton {
    
    [self.navigationController popViewControllerAnimated:YES];
}

//将通知消息接收
- (void)indexPath:(NSNotification *)noti {
    
    [self.tableView reloadData];
    self.index = [noti.userInfo objectForKey:@"indexPath"];
    
}

#pragma mark - tableView 协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.ModelArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    self.model = self.ModelArr[indexPath.row];
    YZLCarRedioListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"redio"];
    if (cell == nil) {
        cell = [[YZLCarRedioListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"redio"];
    }
    cell.model = self.model;
//    NSLog(@"~~%@", self.model.program_fileurl);
    if (!self.result) {
        if ([self.selectedIndexPath isEqual:indexPath]){
            cell.titleLabel.textColor = [UIColor blueColor];
            cell.timeLabel.textColor = [UIColor blueColor];
            cell.playImage.image = [UIImage imageNamed:@"play"];
        }else {
            cell.titleLabel.textColor = [UIColor blackColor];
            cell.timeLabel.textColor = [UIColor blackColor];
            cell.playImage.image = nil;
        }

    }else {
        if ([self.result isEqual:self.model.program_fileurl]) {
            
            NSLog(@"❀❀❀");
            
            cell.titleLabel.textColor = [UIColor blueColor];
            cell.timeLabel.textColor = [UIColor blueColor];
            cell.playImage.image = [UIImage imageNamed:@"play"];
        }else {
            cell.titleLabel.textColor = [UIColor blackColor];
            cell.timeLabel.textColor = [UIColor blackColor];
            cell.playImage.image = nil;
        }
    }
    //判断是否有通知的消息
    NSInteger indexNum = [self.index integerValue];
    //如果没有 执行cell点击方法中改变颜色
    if (!self.index) {
        if ([self.selectedIndexPath isEqual:indexPath]){
            cell.titleLabel.textColor = [UIColor blueColor];
            cell.timeLabel.textColor = [UIColor blueColor];
            cell.playImage.image = [UIImage imageNamed:@"play"];
        }else {
            cell.titleLabel.textColor = [UIColor blackColor];
            cell.timeLabel.textColor = [UIColor blackColor];
            cell.playImage.image = nil;
        }
    } else {//如果存在通知 执行下面改变颜色的方法
        if (indexNum == indexPath.row){
            cell.titleLabel.textColor = [UIColor blueColor];
            cell.timeLabel.textColor = [UIColor blueColor];
            cell.playImage.image = [UIImage imageNamed:@"play"];
        }else {
            cell.titleLabel.textColor = [UIColor blackColor];
            cell.timeLabel.textColor = [UIColor blackColor];
            cell.playImage.image = nil;
        }
    }
    
    return cell;
}

#pragma mark - cell点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    YZLCarRedioPlayingViewController *vc = [[YZLCarRedioPlayingViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
    vc.model = self.ModelArr[indexPath.row];
    vc.imageStr = self.album_bigImgurlStr;
    vc.num = indexPath.row;
    vc.RedioArr = self.ModelArr;
    
    if (self.selectedIndexPath) {
        YZLCarRedioListCell *cell = [tableView cellForRowAtIndexPath:self.selectedIndexPath];
        cell.titleLabel.textColor = [UIColor blackColor];
        cell.timeLabel.textColor = [UIColor blackColor];
        cell.playImage.image = nil;
    }
    YZLCarRedioListCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.titleLabel.textColor = [UIColor blueColor];
    cell.timeLabel.textColor = [UIColor blueColor];
    cell.playImage.image = [UIImage imageNamed:@"play"];
    self.selectedIndexPath = indexPath;
}

#pragma mark - 隐藏tabBar
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}

#pragma mark -
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
